package lol.clann.minecraft.springboot.plugin.core;

import com.google.auto.service.AutoService;
import lol.clann.minecraft.springboot.api.spi.sbp.ApplicationRunListener;
import lol.clann.minecraft.springboot.bukkit.utils.BukkitUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;

import javax.management.MBeanTrustPermission;
import java.security.Permission;
import java.security.Policy;
import java.security.ProtectionDomain;

/**
 *
 * @author pyz
 * @date 2019/6/26 8:29 PM
 */
@Slf4j
@AutoService(ApplicationRunListener.class)
public class PolicyReplace implements ApplicationRunListener {
    @Override
    public void starting(SpringApplication application) {
        if (BukkitUtils.getBukkitVersionNumber() < 10800) {
            LOGGER.info("当前服务器版本低于1.8,替换默认Policy");
            Policy.setPolicy(new Policy() {
                @Override
                public boolean implies(ProtectionDomain domain, Permission permission) {
                    if (permission instanceof MBeanTrustPermission) {
                        return true;
                    }
                    return super.implies(domain, permission);
                }
            });
        }
    }
}
